home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / libtiff / tif_open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-07  |  8.1 KB  |  345 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_open.c,v 1.29 91/08/19 14:40:45 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991 Sam Leffler
  7.  * Copyright (c) 1991 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  */
  32. #include "tiffioP.h"
  33. #include "prototypes.h"
  34.  
  35. #define    ord(e)    ((int)e)
  36.  
  37. #if USE_PROTOTYPES
  38. extern    int TIFFDefaultDirectory(TIFF*);
  39. #else
  40. extern    int TIFFDefaultDirectory();
  41. #endif
  42.  
  43. /*
  44.  * Initialize the bit fill order, the
  45.  * shift & mask tables, and the byte
  46.  * swapping state according to the file
  47.  * contents and the machine architecture.
  48.  */
  49. static
  50. DECLARE3(TIFFInitOrder, register TIFF*, tif, int, magic, int, bigendian)
  51. {
  52.     /* XXX how can we deduce this dynamically? */
  53.     tif->tif_fillorder = FILLORDER_MSB2LSB;
  54.  
  55.     tif->tif_typemask[0] = 0;
  56.     tif->tif_typemask[ord(TIFF_BYTE)] = 0xff;
  57.     tif->tif_typemask[ord(TIFF_SHORT)] = 0xffff;
  58.     tif->tif_typemask[ord(TIFF_LONG)] = 0xffffffff;
  59.     tif->tif_typemask[ord(TIFF_RATIONAL)] = 0xffffffff;
  60.     tif->tif_typeshift[0] = 0;
  61.     tif->tif_typeshift[ord(TIFF_LONG)] = 0;
  62.     tif->tif_typeshift[ord(TIFF_RATIONAL)] = 0;
  63.     if (magic == TIFF_BIGENDIAN) {
  64.         tif->tif_typeshift[ord(TIFF_BYTE)] = 24;
  65.         tif->tif_typeshift[ord(TIFF_SHORT)] = 16;
  66.         if (!bigendian)
  67.             tif->tif_flags |= TIFF_SWAB;
  68.     } else {
  69.         tif->tif_typeshift[ord(TIFF_BYTE)] = 0;
  70.         tif->tif_typeshift[ord(TIFF_SHORT)] = 0;
  71.         if (bigendian)
  72.             tif->tif_flags |= TIFF_SWAB;
  73.     }
  74. }
  75.  
  76. static int
  77. DECLARE2(getMode, char*, mode, char*, module)
  78. {
  79.     int m = -1;
  80.  
  81.     switch (mode[0]) {
  82.     case 'r':
  83.         m = O_RDONLY;
  84.         if (mode[1] == '+')
  85.             m = O_RDWR;
  86.         break;
  87.     case 'w':
  88.     case 'a':
  89.         m = O_RDWR|O_CREAT;
  90.         if (mode[0] == 'w')
  91.             m |= O_TRUNC;
  92.         break;
  93.     default:
  94.         TIFFError(module, "\"%s\": Bad mode", mode);
  95.         break;
  96.     }
  97.     return (m);
  98. }
  99.  
  100. /*
  101.  * Open a TIFF file for read/writing.
  102.  */
  103. TIFF *
  104. TIFFOpen(name, mode)
  105.     char *name, *mode;
  106. {
  107.     static char module[] = "TIFFOpen";
  108.     int m, fd;
  109.  
  110.     m = getMode(mode, module);
  111.     if (m == -1)
  112.         return ((TIFF *)0);
  113.     fd = TIFFOpenFile(name, m, 0666);
  114.     if (fd < 0) {
  115.         TIFFError(module, "%s: Cannot open", name);
  116.         return ((TIFF *)0);
  117.     }
  118.     return (TIFFFdOpen(fd, name, mode));
  119. }
  120.  
  121. /*
  122.  * Open a TIFF file descriptor for read/writing.
  123.  */
  124. TIFF *
  125. TIFFFdOpen(fd, name, mode)
  126.     int fd;
  127.     char *name, *mode;
  128. {
  129.     static char module[] = "TIFFFdOpen";
  130.     TIFF *tif;
  131.     int m, bigendian;
  132.  
  133.     m = getMode(mode, module);
  134.     if (m == -1)
  135.         goto bad2;
  136.     tif = (TIFF *)malloc(sizeof (TIFF) + strlen(name) + 1);
  137.     if (tif == NULL) {
  138.         TIFFError(module, "%s: Out of memory (TIFF structure)", name);
  139.         goto bad2;
  140.     }
  141.     bzero((char *)tif, sizeof (*tif));
  142.     tif->tif_name = (char *)tif + sizeof (TIFF);
  143.     strcpy(tif->tif_name, name);
  144.     tif->tif_fd = fd;
  145.     tif->tif_mode = m &~ (O_CREAT|O_TRUNC);
  146.     tif->tif_curoff = 0;
  147.     tif->tif_curstrip = -1;        /* invalid strip */
  148.     tif->tif_row = -1;        /* read/write pre-increment */
  149.     { int one = 1; bigendian = (*(char *)&one == 0); }
  150.     /*
  151.      * Read in TIFF header.
  152.      */
  153.     if (!ReadOK(fd, &tif->tif_header, sizeof (TIFFHeader))) {
  154.         if (tif->tif_mode == O_RDONLY) {
  155.             TIFFError(name, "Cannot read TIFF header");
  156.             goto bad;
  157.         }
  158.         /*
  159.          * Setup header and write.
  160.          */
  161.         tif->tif_header.tiff_magic =  bigendian ?
  162.             TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
  163.         tif->tif_header.tiff_version = TIFF_VERSION;
  164.         tif->tif_header.tiff_diroff = 0;    /* filled in later */
  165.         if (!WriteOK(fd, &tif->tif_header, sizeof (TIFFHeader))) {
  166.             TIFFError(name, "Error writing TIFF header");
  167.             goto bad;
  168.         }
  169.         /*
  170.          * Setup the byte order handling.
  171.          */
  172.         TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
  173.         /*
  174.          * Setup default directory.
  175.          */
  176.         if (!TIFFDefaultDirectory(tif))
  177.             goto bad;
  178.         tif->tif_diroff = 0;
  179.         return (tif);
  180.     }
  181.     /*
  182.      * Setup the byte order handling.
  183.      */
  184.     if (tif->tif_header.tiff_magic != TIFF_BIGENDIAN &&
  185.         tif->tif_header.tiff_magic != TIFF_LITTLEENDIAN) {
  186.         TIFFError(name,  "Not a TIFF file, bad magic number %d (0x%x)",
  187.             tif->tif_header.tiff_magic,
  188.             tif->tif_header.tiff_magic);
  189.         goto bad;
  190.     }
  191.     TIFFInitOrder(tif, tif->tif_header.tiff_magic, bigendian);
  192.     /*
  193.      * Swap header if required.
  194.      */
  195.     if (tif->tif_flags & TIFF_SWAB) {
  196.         TIFFSwabShort(&tif->tif_header.tiff_version);
  197.         TIFFSwabLong(&tif->tif_header.tiff_diroff);
  198.     }
  199.     /*
  200.      * Now check version (if needed, it's been byte-swapped).
  201.      * Note that this isn't actually a version number, it's a
  202.      * magic number that doesn't change (stupid).
  203.      */
  204.     if (tif->tif_header.tiff_version != TIFF_VERSION) {
  205.         TIFFError(name,
  206.             "Not a TIFF file, bad version number %d (0x%x)",
  207.             tif->tif_header.tiff_version,
  208.             tif->tif_header.tiff_version); 
  209.         goto bad;
  210.     }
  211.     tif->tif_flags |= TIFF_MYBUFFER;
  212.     tif->tif_rawcp = tif->tif_rawdata = 0;
  213.     tif->tif_rawdatasize = 0;
  214.     /*
  215.      * Setup initial directory.
  216.      */
  217.     switch (mode[0]) {
  218.     case 'r':
  219.         tif->tif_nextdiroff = tif->tif_header.tiff_diroff;
  220. #ifdef MMAP_SUPPORT
  221.         if (TIFFMapFileContents(fd, &tif->tif_base, &tif->tif_size))
  222.             tif->tif_flags |= TIFF_MAPPED;
  223. #endif
  224.         if (TIFFReadDirectory(tif)) {
  225.             tif->tif_rawcc = -1;
  226.             tif->tif_flags |= TIFF_BUFFERSETUP;
  227.             return (tif);
  228.         }
  229.         break;
  230.     case 'a':
  231.         /*
  232.          * Don't append to file that has information
  233.          * byte swapped -- we will write data that is
  234.          * in the opposite order.
  235.          */
  236.         if (tif->tif_flags & TIFF_SWAB) {
  237.             TIFFError(name,
  238.         "Cannot append to file that has opposite byte ordering");
  239.             goto bad;
  240.         }
  241.         /*
  242.          * New directories are automatically append
  243.          * to the end of the directory chain when they
  244.          * are written out (see TIFFWriteDirectory).
  245.          */
  246.         if (!TIFFDefaultDirectory(tif))
  247.             goto bad;
  248.         return (tif);
  249.     }
  250. bad:
  251.     tif->tif_mode = O_RDONLY;    /* XXX avoid flush */
  252.     TIFFClose(tif);
  253.     return ((TIFF *)0);
  254. bad2:
  255.     (void) close(fd);
  256.     return ((TIFF *)0);
  257. }
  258.  
  259. TIFFScanlineSize(tif)
  260.     TIFF *tif;
  261. {
  262.     TIFFDirectory *td = &tif->tif_dir;
  263.     long scanline;
  264.     
  265.     scanline = td->td_bitspersample * td->td_imagewidth;
  266.     if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  267.         scanline *= td->td_samplesperpixel;
  268.     return (howmany(scanline, 8));
  269. }
  270.  
  271. /*
  272.  * Query functions to access private data.
  273.  */
  274.  
  275. /*
  276.  * Return open file's name.
  277.  */
  278. char *
  279. TIFFFileName(tif)
  280.     TIFF *tif;
  281. {
  282.     return (tif->tif_name);
  283. }
  284.  
  285. /*
  286.  * Return open file's I/O descriptor.
  287.  */
  288. int
  289. TIFFFileno(tif)
  290.     TIFF *tif;
  291. {
  292.     return (tif->tif_fd);
  293. }
  294.  
  295. /*
  296.  * Return read/write mode.
  297.  */
  298. int
  299. TIFFGetMode(tif)
  300.     TIFF *tif;
  301. {
  302.     return (tif->tif_mode);
  303. }
  304.  
  305. /*
  306.  * Return nonzero if file is organized in
  307.  * tiles; zero if organized as strips.
  308.  */
  309. int
  310. TIFFIsTiled(tif)
  311.     TIFF *tif;
  312. {
  313.     return (isTiled(tif));
  314. }
  315.  
  316. /*
  317.  * Return current row being read/written.
  318.  */
  319. long
  320. TIFFCurrentRow(tif)
  321.     TIFF *tif;
  322. {
  323.     return (tif->tif_row);
  324. }
  325.  
  326. /*
  327.  * Return current strip.
  328.  */
  329. int
  330. TIFFCurrentStrip(tif)
  331.     TIFF *tif;
  332. {
  333.     return (tif->tif_curstrip);
  334. }
  335.  
  336. /*
  337.  * Return current tile.
  338.  */
  339. int
  340. TIFFCurrentTile(tif)
  341.     TIFF *tif;
  342. {
  343.     return (tif->tif_curtile);
  344. }
  345.